package com.jus.player.adapter.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jus.player.R;
import com.jus.player.activity.home.JsPlayerActivity;
import com.jus.player.config.AppConfig;
import com.jus.player.entity.VideoEntity;
import com.jus.player.util.StringUtils;

import java.util.List;

public class FollowTvAdaptor extends RecyclerView.Adapter<FollowTvAdaptor.ViewHolder> {

    private List<VideoEntity> videos;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_follow_tv, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Context context = holder.itemView.getContext();
        VideoEntity video = videos.get(position);
        // 假设这里使用Glide加载视频缩略图
        if (!StringUtils.isEmpty(video.getTvCoverUrl())) {
            // 先清除旧图片（避免复用问题）
            //Glide.with(holder.itemView).clear(holder.thumbImg);
            Glide.with(context)
                    .load(video.getTvCoverUrl())
                    //.apply(RequestOptions.bitmapTransform(new RoundedCorners(32)))//圆角
                    //.signature(new ObjectKey(System.currentTimeMillis())) // 时间戳作为唯一标识
                    //.skipMemoryCache(true) // 禁用内存缓存
                    //.diskCacheStrategy(DiskCacheStrategy.NONE) // 禁用所有缓存
                    //.transform(new RoundedCorners(32))//滑动页面不能都生效
                    .into(holder.thumbImg);
        }
        holder.title.setText(video.getTitle());
        String str1 = context.getString(R.string.str_update_to);
        String str2 = context.getString(R.string.str_episode);
        holder.updateEpisode.setText(str1 + " " + video.getUpdateEpisode() + " " + str2);
        int lastEpisode = video.getLastEpisode() == null
                ? AppConfig.INT_0 : video.getLastEpisode() - AppConfig.INT_1;
        holder.coverContainer.setOnClickListener(view ->
                JsPlayerActivity.start(holder.coverContainer.getContext(),
                        lastEpisode, video.getId(), false));
    }

    @Override
    public int getItemCount() {
        return videos == null ? 0 : videos.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ConstraintLayout coverContainer;
        public ImageView thumbImg;
        public TextView title;
        public TextView updateEpisode;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            thumbImg = itemView.findViewById(R.id.video_thumb);
            coverContainer = itemView.findViewById(R.id.coverContainer);
            title = itemView.findViewById(R.id.title);
            updateEpisode = itemView.findViewById(R.id.updateEpisode);
        }

    }

    public void setVideos(List<VideoEntity> videos) {
        this.videos = videos;
    }
}
